import json
from os import X_OK
# 文件、程序异常处理、模块

# 文件的打开


# with open("D:\\test.txt", "r", encoding="gbk") as f:
# text = f.read()
# text=f.readline()
# text = f.readlines()
# while True:
#     text =f.readline()
#     if not text:
#         break
#     else:
#         print(text,end="")

# print(text)

# with open("D:\\test.txt", "w", encoding="gbk") as f:
# 追加
# ls=["张三","李四"]
# with open("D:\\test.txt", "w", encoding="utf-8") as f:
#     # f.write("我爱你")  #文件不存在则立即创建一个
#     f.writelines(ls)

# 既读又写
# r+
'''
如果文件名不存在，则报错
指针在开始
要把指针移动到末尾才能开始写，否则会覆盖前面内容
'''
# with open("D:\\test.txt", "w", encoding="utf-8") as f:
#     # for line in f:
#         # print(line) #全部读一遍后，指针会到达末尾
#     f.seek(0,2) #偏移字节数，位置（0，开始；1当前位置，2结束）
#     text=["就撒戴假发","发斯蒂芬","fwer"]
#     f.writelines(text)


# w+
'''
若文件不存在，则创建
若文件存在则，则会立即清空原内容
'''
# with open("D:\\test.txt", "w+", encoding="utf-8") as f:
#     # pass
#     text=["就撒戴假发","发斯蒂芬","fwer"]
#     f.writelines(text)
#     f.seek(0,0) #指针移动到开始
#     print(f.read())

# a+
'''
若文件不存在，则创建
指针在末尾，添加新内容，不会清空原内容
'''
# with open("D:\\test.txt", "a+", encoding="utf-8") as f:
#     # pass
#     text = ["就撒戴假发", "发斯蒂芬", "fwer"]
#     f.writelines(text)
#     f.seek(0, 0)  # 指针移动到开始
#     print(f.read())


# csv写入
# ls=[['编号','数学成绩','语文成绩'],['1','100','100'],['2','90','80']]
# with open("D:\\成绩.csv","w",encoding="gbk") as f:
#     for row in ls :
#         f.write(",".join(row)+"\n")
# csv读取
# with open("D:\\成绩.csv", "r", encoding="gbk")as f:
#     ls=[]
#     for line in f:
#         ls.append(line.strip("\n").split(","))
# for res in ls:
#     print(res)

#import json
# score ={
#     "Petter":{"Math":100,"phsics":99},
#     "Lucy":{"Math":95,"phsics":99}
# }
# with open("D:\\score.json","w",encoding="gbk") as f:
#     #indent表示字符串换行+缩进 ensure_ascii=False显示中文
#     json.dump(score,f,indent=4,ensure_ascii=False)

# with open("D:\\score.json","r",encoding="gbk") as f:
#     score=json.load(f)
#     for k,v in score.items():
#         print(k,v)



#异常

# ls =[]

# try:
#     a= 1/2
# except NameError as e:
#     print(e)
#     print("变量名不存在")
# except Exception:
#     print("parent")
# finally:
#     print("finally")
# # else:
# #     print("else ......")


#模块
#python内置
'''
python内置
时间库time、随机库random、容器数据类型collection、迭代器函数itertools
第三方库
数据分析numpy、pandas
数据可视化matplotlib
机器学习scikit-learn
深度学习Tensorflow
'''
# import time
# start = time.time()
# time.sleep(3)
# end=time.time()
# print("程序运行用时:{:.2f}秒".format(end-start))


# from itertools import product
# ls = list(product("AB","123"))
# print(ls)

# from random import *
# print(randint(1,100))

'''
加载内存中的模块
Python启动时解释器会默认加载一些modules存放在sys.modules
sys.modules 变量包含一个由当前载入到解释器的模块组成的字典，模块名作为键，它们的位置作为值
'''
# import sys
# print(len(sys.modules))
# print("math" in sys.modules)
# print("numpy" in sys.modules)
# print(sys.path)
'''
sys.path的第一个路径是当前执行文件所在的文件夹
若需将在不在该文件夹的模块导入，需要将模块的路径添加到sys.path
'''
# import sys
# sys.path.append("C:\\Users\\x\\Desktop")
# import fun3 